import tkinter as tk
from PIL import Image, ImageTk
from tkinter import ttk,messagebox


class CreateNewQuestionnaire(tk.Toplevel):
    def __init__(self, parent):
        super().__init__()

        self.parent = parent
        self.title("问卷详情")

        # 获取屏幕的宽和高
        sreen_width = self.winfo_screenwidth()
        screen_height = self.winfo_screenheight()

        # 设置窗口的宽和高
        root_width = int(sreen_width / 2)
        root_height = int(screen_height / 2)

        # 使窗口居中
        self.geometry("{}x{}+{}+{}".format(
            root_width, root_height,
            int((sreen_width - root_width) / 2), int((screen_height - root_height) / 2)
        ))


        # # 使用 Pillow 加载图像
        # image = Image.open("Image/return.png")
        # # 调整图像大小
        # new_size = (25, 25)  # 设置新的尺寸 (宽度, 高度)
        # resized_image = image.resize(new_size, Image.LANCZOS)
        # photo = ImageTk.PhotoImage(resized_image)
        #
        # self.back_button = tk.Button(self,image=photo , text="返回",font=("宋体", 15), compound='left',command=self.back_to_main)
        # self.back_button.photo = photo  # 保存图像对象的引用
        # self.back_button.place(relx=0.01,rely=0.01,relwidth=0.15,relheight=0.1)

    #     # # 创建主框架，包含添加问题按钮和滚动区域
    #     # main_frame = tk.Frame(root)
    #     # main_frame.pack(fill=tk.BOTH, expand=True)
    #
    #     self.add_question_button = tk.Button(self, text="添加问题", command=self.add_question)
    #     self.add_question_button.pack(pady=10)
    #
    #     # 创建滚动区域
    #     self.canvas = tk.Canvas(self)
    #     self.scroll_y = tk.Scrollbar(self, orient="vertical", command=self.canvas.yview)
    #
    #     self.scrollable_frame = ttk.Frame(self.canvas)
    #     self.scrollable_frame.bind(
    #         "<Configure>",
    #         lambda e: self.canvas.configure(
    #             scrollregion=self.canvas.bbox("all")
    #         )
    #     )
    #
    #     self.canvas.create_window((0, 0), window=self.scrollable_frame, anchor="nw")
    #     self.canvas.configure(yscrollcommand=self.scroll_y.set)
    #
    #     self.canvas.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
    #     self.scroll_y.pack(side=tk.RIGHT, fill=tk.Y)
    #
    #     # # 创建底部框架，包含确认保存按钮
    #     # bottom_frame = tk.Frame(root)
    #     # bottom_frame.pack(side=tk.BOTTOM, fill=tk.X)
    #
    #     self.save_button = tk.Button(self, text="确认保存",font=("宋体", 15), command=self.on_save)
    #     self.save_button.pack(side=tk.RIGHT, padx=5, pady=5)
    #
    #     # 初始化问题计数器
    #     self.question_counter = 0
    #
    # def back_to_main(self):
    #     self.destroy()
    #     self.parent.deiconify()
    #
    # def add_question(self):
    #     self.question_counter += 1
    #
    #     # # 创建一个新的问题框架
    #     # question_frame = tk.Frame(self.scrollable_frame, pady=5)
    #     # question_frame.pack(fill=tk.X)
    #
    #     # 添加问题标签和输入框
    #     self.question_label = tk.Label(self, text=f"问题 {self.question_counter}:")
    #     self.question_label.pack(side=tk.LEFT, padx=5)
    #
    #     self.question_entry = tk.Entry(self)
    #     self.question_entry.pack(side=tk.LEFT, fill=tk.X, expand=True, padx=5)
    #
    #     # 添加选项单选按钮
    #     self.option_var = tk.StringVar(value="选项 1")
    #
    #     self.option_1 = tk.Radiobutton(self, text="选项 1", variable=self.option_var, value="选项 1")
    #     self.option_1.pack(side=tk.LEFT, padx=5)
    #
    #     self.option_2 = tk.Radiobutton(self, text="选项 2", variable=self.option_var, value="选项 2")
    #     self.option_2.pack(side=tk.LEFT, padx=5)
    #
    # def on_save(self):
    #     print("确认保存按钮点击")
        self.canvas = tk.Canvas(self)
        self.scrollbar = ttk.Scrollbar(self, orient="vertical", command=self.canvas.yview)
        self.scroll_frame = ttk.Frame(self.canvas)

        self.scroll_frame.bind(
            "<Configure>",
            lambda e: self.canvas.configure(
                scrollregion=self.canvas.bbox("all")
            )
        )

        self.canvas.create_window((0, 0), window=self.scroll_frame, anchor="nw")
        self.canvas.configure(yscrollcommand=self.scrollbar.set)

        self.canvas.place(relx=0, rely=0.2, relwidth=0.95, relheight=0.8)
        self.scrollbar.place(relx=0.95, rely=0.2, relwidth=0.05, relheight=0.8)

        self.questionnaire = []
        self.deadline = []
        self.question_count = 0
        self.questions = []

        # 使用 Pillow 加载图像
        image = Image.open("Image/return.png")
        # 调整图像大小
        new_size = (25, 25)  # 设置新的尺寸 (宽度, 高度)
        resized_image = image.resize(new_size, Image.LANCZOS)
        photo = ImageTk.PhotoImage(resized_image)

        # 创建返回按钮
        self.back_button = tk.Button(self,image=photo , text="返回",font=("宋体", 15), compound='left',command=self.back_to_main)
        self.back_button.photo = photo  # 保存图像对象的引用
        self.back_button.place(relx=0.01,rely=0.01,relwidth=0.15,relheight=0.1)

        # 创建添加问题按钮
        self.add_btn = tk.Button(self, text="添加问题",font=("宋体", 15), command=self.add_question)
        self.add_btn.place(relx=0.4,rely=0.01,relwidth=0.15,relheight=0.1)

        # 创建提交按钮
        self.confirm_btn = tk.Button(self, text="提交",font=("宋体", 15), command=self.confirm)
        self.confirm_btn.place(relx=0.84,rely=0.01,relwidth=0.15,relheight=0.1)

        # 问卷名称及输入框
        self.questionnaire_label = tk.Label(self,text="问卷名称")
        self.questionnaire_entry = tk.Entry(self,width=4)

        # 问卷名称及输入框几何管理
        self.questionnaire_label.place(relx=0.01,rely=0.1,relwidth=0.15,relheight=0.1)
        self.questionnaire_entry.place(relx=0.15,rely=0.11,relwidth=0.15,relheight=0.08)

        self.questionnaire.append(self.questionnaire_entry)

        # 截止时间及输入框
        self.deadline_label = tk.Label(self,text="截止时间")
        self.deadline_entry = tk.Entry(self,width=4)

        # 截止时间及输入框几何管理
        self.deadline_label.place(relx=0.6,rely=0.1,relwidth=0.15,relheight=0.1)
        self.deadline_entry.place(relx=0.75,rely=0.11,relwidth=0.15,relheight=0.08)

        self.deadline.append(self.deadline_entry)

    def add_question(self):
        # 问题名称及输入框
        question_label = tk.Label(self.scroll_frame, text=f"Question {self.question_count + 1}")
        question_entry = tk.Entry(self.scroll_frame, width=20)

        # 问题名称及输入框几何管理
        question_label.grid(row=self.question_count * 5, column=0, padx=10, pady=5, sticky='w')
        question_entry.grid(row=self.question_count * 5, column=1, padx=10, pady=5)

        var = tk.IntVar()
        options = []
        for i in range(4):
            option_entry = tk.Entry(self.scroll_frame, width=20)
            option_entry.grid(row=self.question_count * 5 + i, column=4, padx=10, pady=5, sticky="w")
            options.append(option_entry)
            radio_btn = tk.Radiobutton(self.scroll_frame, variable=var, value=i + 1)
            radio_btn.grid(row=self.question_count * 5 + i, column=3, padx=10, pady=5, sticky="w")

        self.questions.append((question_entry, options, var))

        self.question_count += 1

        # 更新画布滚动区域
        self.canvas.configure(scrollregion=self.canvas.bbox("all"))

    def confirm(self):
        questionnaire_text = self.questionnaire_entry.get()
        print(f"问卷名称: {questionnaire_text}")
        deadline_text = self.deadline_entry.get()
        print(f"截止时间: {deadline_text}")
        print(f"问题数量: {self.question_count}")
        # 确认和保存问题的逻辑
        for idx, (question, options, correct_var) in enumerate(self.questions):
            question_text = question.get()
            options_text = [opt.get() for opt in options]
            correct_option = correct_var.get()
            print(f"问题 {idx + 1}: {question_text}")
            print(f"选项: {options_text}")
            print(f"正确的选项: {correct_option}")

    # 返回按钮功能
    def back_to_main(self):
        self.destroy()
        self.parent.deiconify()


    # 提交键功能集合
    def submit_key_function(self):
        self.on_submit()

    # 提交键功能
    def on_submit(self):
        messagebox.showinfo('Info', '提交完成')